/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.psc.controllers;

import com.mycompany.psc.helpers.DBUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.annotation.Resource;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author A7med
 */
@Named
@SessionScoped
public class RestoreController implements Serializable{

    private UploadedFile file;
    private String masterPassword;
    private boolean authtincatedRestore;
    @Resource(lookup = "PROJECTS_FOLDER")
    private Properties p;
    @Inject
    DBUtils dbUtils;

    public String getMasterPassword() {
        return masterPassword;
    }

    public void setMasterPassword(String masterPassword) {
        this.masterPassword = masterPassword;
    }

    public boolean isAuthtincatedRestore() {
        return authtincatedRestore;
    }

    public void setAuthtincatedRestore(boolean authtincatedRestore) {
        this.authtincatedRestore = authtincatedRestore;
    }

    public void setUploadedFile(FileUploadEvent event) {
        
        file = event.getFile();
        System.out.println("file="+file.getFileName());
    }

    public void checkRestore() {
        System.out.println("dddddddddddaaaaaaaaaaaa");
        FacesContext fc = FacesContext.getCurrentInstance();
        if (masterPassword.equals("ABCdef")) {
            authtincatedRestore = true;
            fc.addMessage("", new FacesMessage(FacesMessage.SEVERITY_INFO, "Correct password", "you can proceed with restore operation now"));
        } else {
            authtincatedRestore = false;
            fc.addMessage("", new FacesMessage(FacesMessage.SEVERITY_ERROR, "wrong password", "try again"));

        }

    }

    public void restore() {
        System.out.println("ssssssssssss"+file.getContentType());
        String base = p.getProperty("uri");
        System.out.println("ssssssssssss"+base);
      try {
            unZipIt(file.getInputstream(), base);
            dbUtils.restoreDB(base+"/psc.sql");
        } catch (IOException ex) {
            Logger.getLogger(RestoreController.class.getName()).log(Level.SEVERE, null, ex);
        }
        

    }
     private void unZipIt(InputStream zipFile, String outputFolder){
 
     byte[] buffer = new byte[1024];
 
     try{
 
    	//create output directory is not exists
    	File folder = new File(outputFolder);
    	if(!folder.exists()){
    		folder.mkdir();
    	}
 
    	//get the zip file content
    	ZipInputStream zis = 
    		new ZipInputStream(zipFile);
    	//get the zipped file list entry
    	ZipEntry ze = zis.getNextEntry();
 
    	while(ze!=null){
 
    	   String fileName = ze.getName();
           File newFile = new File(outputFolder + File.separator + fileName);
 
           System.out.println("file unzip : "+ newFile.getAbsoluteFile());
 
            //create all non exists folders
            //else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();
 
            FileOutputStream fos = new FileOutputStream(newFile);             
 
            int len;
            while ((len = zis.read(buffer)) > 0) {
       		fos.write(buffer, 0, len);
            }
 
            fos.close();   
            ze = zis.getNextEntry();
    	}
 
        zis.closeEntry();
    	zis.close();
 
    	System.out.println("Done");
 
    }catch(IOException ex){
       ex.printStackTrace(); 
    }
   } 
}
